import js from "@eslint/js";
import eslintConfigPrettier from "eslint-config-prettier";
import pluginVue from "eslint-plugin-vue";
import { defineConfig } from "eslint/config";
import globals from "globals";
import tseslint from "typescript-eslint";

export default defineConfig([
  // 忽略构建文件夹和 Yarn Plug'n'Play 路径
  {
    ignores: ["node_modules/", ".yarn/", "dist/", "build/"],
  },

  // JavaScript 基础规则 + PnP 导入支持
  {
    files: ["**/*.{js,mjs,cjs,ts,mts,cts,vue}"],
    plugins: { js },
    extends: ["js/recommended"],
    settings: {
      "import/resolver": "eslint-import-resolver-pnp",
    },
  },

  // 设置全局变量（浏览器 + Node）
  {
    files: ["**/*.{js,mjs,cjs,ts,mts,cts,vue}"],
    languageOptions: {
      globals: { ...globals.browser, ...globals.node },
    },
  },

  // TypeScript 推荐规则（不需要类型信息）
  tseslint.configs.recommended,

  // Vue 基础规则
  pluginVue.configs["flat/essential"],

  // 支持 <script lang="ts"> 的 Vue 文件解析
  {
    files: ["**/*.vue"],
    languageOptions: {
      parserOptions: { parser: tseslint.parser },
    },
  },

  // ✅ 通用规则（质量 + 风格）
  {
    files: ["**/*.{js,mjs,cjs,ts,mts,cts,vue}"],
    rules: {
      // ✅ 基础代码质量规则
      "no-console": "warn",
      "no-debugger": "error",
      "no-alert": "error",
      "no-eval": "error",
      "no-implied-eval": "error",
      "no-with": "error",
      "no-var": "error",
      "prefer-const": "error",
      "prefer-arrow-callback": "warn",
      "no-multiple-empty-lines": ["error", { max: 2, maxEOF: 1 }],
      eqeqeq: "error",
      curly: "error",
      camelcase: "warn",
      "no-unused-expressions": "error",
      "consistent-return": "error",
      "default-case": "warn",

      // ✅ TypeScript 语法相关规则（无需类型信息）
      "@typescript-eslint/no-explicit-any": "warn", // 实用性更强
      "@typescript-eslint/no-unused-vars": [
        "error",
        {
          argsIgnorePattern: "^_",
          varsIgnorePattern: "^_",
          caughtErrorsIgnorePattern: "^_",
        },
      ],
      "@typescript-eslint/explicit-function-return-type": "off",
      "@typescript-eslint/no-non-null-assertion": "warn",
      "@typescript-eslint/no-empty-function": "warn",
      "@typescript-eslint/no-inferrable-types": "error",
    },
  },

  // ✅ TypeScript 类型相关规则（启用 projectService）
  {
    files: ["**/*.{ts,tsx,vue}"],
    languageOptions: {
      parserOptions: {
        projectService: true,
        tsconfigRootDir: process.cwd(),
        extraFileExtensions: [".vue"],
      },
    },
    rules: {
      "@typescript-eslint/prefer-nullish-coalescing": "error",
      "@typescript-eslint/prefer-optional-chain": "error",
      "@typescript-eslint/no-floating-promises": "warn",
      "@typescript-eslint/await-thenable": "error",
      "@typescript-eslint/no-misused-promises": "error",
    },
  },

  // ✅ Vue 特定规则
  {
    files: ["**/*.vue"],
    rules: {
      "vue/multi-word-component-names": "warn", // 单词组件名发出警告
      "vue/component-name-in-template-casing": ["error", "PascalCase"],
      "vue/max-attributes-per-line": ["error", { singleline: 3, multiline: 1 }],
      "vue/no-multiple-template-root": "off",
      "vue/attribute-hyphenation": ["error", "always"],
      "vue/v-on-event-hyphenation": ["error", "always"],
      "vue/no-useless-template-attributes": "error", // 新增：避免 template 中多余属性
    },
  },

  // ✅ 最后加载 Prettier，关闭格式相关规则冲突
  eslintConfigPrettier,
]);
